package com.sgxy.aclservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sgxy.aclservice.entity.AclEmp;
import com.sgxy.aclservice.entity.AclRole;
import com.sgxy.aclservice.entity.vo.AclEmpVo;
import com.sgxy.aclservice.service.AclEmpService;
import com.sgxy.aclservice.service.AclRoleService;
import com.sgxy.commonutils.JwtUtils;
import com.sgxy.commonutils.MD5;
import com.sgxy.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 用户(员工) 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-26
 */
@RestController
@RequestMapping("/aclservice/aclemp")
//@CrossOrigin
public class AclEmpController {
    @Autowired
    AclEmpService aclEmpService;
    @Autowired
    AclRoleService aclRoleService;


    @PostMapping("login")
    public R loginUser(@RequestBody AclEmp aclEmp){
        //return R.ok().data("token","admin");
        String token = aclEmpService.login(aclEmp);
        return R.ok().data("token",token);
    }
    @PostMapping("logout")
    public R logout(){
        return R.ok();
    }
    //
    // todo 根据token查找用户
    @GetMapping("info")
    public R info(HttpServletRequest request){

        //调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String empId = JwtUtils.getMemberIdByJwtToken(request);
        //System.out.println(empId);
        //查询数据库的用户
        AclEmp user = aclEmpService.getById(empId);
        //查询当前用户的角色
        QueryWrapper<AclRole> wrapper = new QueryWrapper<>();
        AclRole role = aclRoleService.getOne(wrapper);
        //return R.ok().data("userInfo",user);
        return R.ok().data("roles","["+role.getRoleName()+"]").data("name",user.getNickName()).data("avatar",null);
    }
    //获取员工完整信息
    @GetMapping("getEmpInfoById/{id}")
    public R getEmpInfoById(@PathVariable String id){
        AclEmpVo aclEmpVo = aclEmpService.getEmpInfoById(id);
        return R.ok().data("aclEmpVo",aclEmpVo);
    }
    //获取员工完整信息分页列表
    @PostMapping("{page}/{limit}")
    public R index(@PathVariable Long page,
                   @PathVariable Long limit,
                   @RequestBody(required = false)AclEmp aclEmp){
        if (aclEmp==null)
            aclEmp = new AclEmp();
        Page<AclEmpVo> empPage = new Page<>(page,limit);
        aclEmpService.selectPageVoList(empPage, aclEmp);
        //System.out.println(aclEmpVos);
        //System.out.println(empPage.getRecords());
        return R.ok().data("items",empPage.getRecords()).data("total",empPage.getTotal());
    }
    //获取员工基本信息
    @GetMapping("getEmpAll")
    public R getEmpById(){
        List<AclEmp> aclEmps = aclEmpService.list(null);
        return R.ok().data("aclEmpList",aclEmps);
    }

    //添加
    @PostMapping("addEmp")
    public R  addEmp(@RequestBody AclEmp aclEmp){
        System.out.println(aclEmp);
        aclEmp.setPassword(MD5.encrypt(aclEmp.getPassword()));
        aclEmpService.save(aclEmp);
        return R.ok();
    }
    //更新
    @PostMapping("updateEmp")
    public R updateEmp(@RequestBody AclEmp aclEmp){
        System.out.println(aclEmp);
        aclEmpService.updateById(aclEmp);
        return R.ok();
    }
    @DeleteMapping("{id}")
    public R deleteEmpById(@PathVariable String id){
        aclEmpService.removeById(id);
        return R.ok();
    }


}

